package com.yuntsg.ruiijn.paperana.utils;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.Date;

/**
 * @Author: gcr
 * @Date: 2023/10/18 17:00
 */
public class DateUtil {


    /**
     * 输入字符时间 yyyy-mm-dd 返回long
     *
     * @param str
     * @return
     */
    public static Long StringToDate(String str) {
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate localDate = LocalDate.parse(str, df);
        long l = localDate.atStartOfDay(ZoneOffset.ofHours(8)).toInstant().toEpochMilli();
        return l;
    }

    /**
     * 输入long  返回LocalDateTime
     *
     * @param str
     * @return
     */
    public static LocalDateTime StringToDateTime(Long str) {
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String s = longToDate1(str);
        LocalDateTime localDateTime = LocalDateTime.parse(s, df);
        return localDateTime;
    }

    /**
     * 输入long  返回LocalDateTime
     *
     * @param str
     * @return
     */
    public static LocalDateTime StringToDateTimeHM(Long str) {
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
        String s = longToDate1(str);
        LocalDateTime localDateTime = LocalDateTime.parse(s, df);
        return localDateTime;
    }


    /**
     * 输入long 返回年月日 时分秒
     *
     * @param i
     * @return
     */
    public static String longToDate1(long i) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date(i);
        String str = sdf.format(date);
        return str;
    }

    /**
     * 输入long 返回年月日 时分秒
     *
     * @param i
     * @return
     */
    public static String longToDate1HM(long i) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        Date date = new Date(i);
        String str = sdf.format(date);
        return str;
    }

    /**
     * 输入 localDateTime 返回yyyy-MM-dd hh:mm
     *
     * @param localDateTime
     * @return
     */
    public static String LocalDateToString(LocalDateTime localDateTime) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
        String res = localDateTime.format(formatter);
        return res;
    }

    /**
     * 输入localdate 返回yyyy年MM月
     *
     * @param localDate
     * @return
     */
    public static String LocalDateToStringyyyymm(LocalDateTime localDate) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        String res = localDate.format(formatter);
        return res;
    }


}
